package com.demo.multThread;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {
    private int port = 9999, password;
    public ServerSocket serverSocket;

    public static void main(String[] args) {

    }

    public Server(int port, int password) {
        this.port = port;
        this.password = password;
        try {
            this.serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

// 连接处理
class getConn implements Runnable {
    private ServerSocket serverSocket;

    public getConn(ServerSocket serverSocket) {
        this.serverSocket = serverSocket;
    }

    //创建一个线程池
    ExecutorService executorService = Executors.newFixedThreadPool(100);

    @Override
    public void run() {
        try {
            while (true) {
                Socket socket = serverSocket.accept();
                executorService.submit(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("Client " + socket.getInetAddress().getHostAddress() + " 连接成功");
                        new Thread(new sendImg(socket)).start();
                    }
                });
            }
        } catch (Exception e) {
            System.out.println("停止连接获取");
        }
    }
}

// try {
//         while (true) {
//         Socket socket = serverSocket.accept();
//         System.out.println(socket.getInetAddress().getHostAddress() + " 连接成功");
//         new Thread(new sendImg(socket)).start();
//         }
//         } catch (Exception e) {
//         e.printStackTrace();
//         }

// while (true) {
//         // Socket socket = null;
//         try {
//         Socket socket = serverSocket.accept();
//         Runnable runnable = () -> {
//         System.out.println(socket.getInetAddress().getHostAddress() + " 连接成功");
//         new sendImg(socket);
//         };
//         executorService.submit(runnable);
//         } catch (IOException e) {
//         e.printStackTrace();
//         }
//         // new Thread(new sendImg(socket)).start();
//         }

// 发送图片
class sendImg implements Runnable {
    private Socket socket;
    long s1, e1;

    public sendImg(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            Robot robot = new Robot();
            // 获取屏幕大小
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle rect = new Rectangle(screen);


            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            // 获取屏幕图片
            BufferedImage img = robot.createScreenCapture(rect);
            s1 = System.currentTimeMillis();
            ImageIO.write(img, "png", byteArrayOutputStream);
            e1 = System.currentTimeMillis();
            System.out.println("Server Write: " + (e1 - s1));
            out.write(byteArrayOutputStream.toByteArray());

            // byteArrayOutputStream.close();
            socket.shutdownOutput();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}